/**
 * Created by chengcong on 2017/11/13.
 */
// var list=[
//     {name:'客服a',time:'刚刚',status:0},
//     {name:'客服b',time:'1分钟前',status:0},
//     {name:'客服c',time:'2分钟前',status:0},
//     {name:'客服d',time:'3分钟前',status:0},
//     {name:'客服e',time:'4分钟前',status:1},
//     {name:'客服f',time:'5分钟前',status:0},
//     {name:'客服g',time:'6分钟前',status:0},
//     {name:'客服h',time:'7分钟前',status:1},
// ];
// const sokceturl = "http://47.90.123.205:8000";
var invitList = [];
var mysocketid = '';
var date_cc = '';
var canemit = true;
socket.on('connect', function(data) {
	console.log(socket.id);
	mysocketid = socket.id;
});
var msg = window.sessionStorage['user_msg'];
msg = JSON.parse(msg);
console.log(msg);
var roomid = '';
$(function() {
	$.ajax({
		url: url + '/service/findinviteroom',
		type: 'post',
		data: {
			id: msg.userid,
			limit: 10,
			page: 1
		},
		headers: {
			'Authorization': 'Bearer ' + msg.token
		},
		success: function(d2) {
			console.log(d2);
			var str = '';
            if(d2.content.length==0){
                str='<p class="text-center"><img src="../../img/empty-b.png" alt="" style="margin-top: 200px"></p>'
            }
			$('#home').html(str);
			var stdiv = '';
			for(var i = 0; i < d2.content.length; i++) {
				var str3 = '';
				if(parseInt(d2.content[i].results) == 1) {
					str3 = '<span style="color: green">已接受</span>';
				}
				if(parseInt(d2.content[i].results) == 2) {
					str3 = '<span style="color: red">已拒绝</span>';
				}
				if(parseInt(d2.content[i].results) == 3) {
					str3 = '<span style="color: red">已关闭</span>';
				}
				if(parseInt(d2.content[i].results) == 0) {
					str3 = '<button type="button" class="btn" ' +
						'style="background:#1abb9c;border: none;width: 86px;color: #fff;font-size: 14px"' +
						' roomid="' + d2.content[i].roomID + '" onclick="joinroom(event)" roomname="'+d2.content[i].roomname+'" _id="' + d2.content[i]._id + '" socketid="' + d2.content[i].socket_id + '">立即进入</button>' +
						' <button type="button" class="btn"' +
						'style="background:#fff;border: 1px solid #1abb9c;width: 86px;color: #1abb9c;font-size: 14px"' +
						' onclick="notjoin(event)" _id="' + d2.content[i]._id + '" socketid="' + d2.content[i].socket_id + '">拒绝</button> ';
				}
				stdiv += '<div class="row bg-color"><div class="col-md-6"><div class="leftmsg"> ' +
					'<p>客服' + d2.content[i].username + ' <span>邀请你加入小组聊天</span></p>' +
					' <div class="rightmsg">' + d2.content[i].time + '</div> ' +
					' </div>' +
					'</div>' +
					' <div class="col-md-6 text-center"> ' + str3 +
					'</div></div>';
			}
			str += stdiv;
			var n = Math.ceil(d2.count / 10);
			if(n > 5) {
				var html = '<div><nav aria-label="Page navigation"><ul class="pagination"><li><a href="#" aria-label="Previous"><span aria-hidden="true">上一页</span></a></li>';
				html += '<li class="active"><a href="#">1</a></li>' +
					'<li><a href="#">2</a></li><li><a href="#">3</a></li>' +
					'<li><a href="#">4</a></li><li><a href="#">5</a></li>';
				html += '<li><a href="#" aria-label="Next"><span aria-hidden="true">下一页</span></a></li> </ul></nav></div>';
			} else {
				var html = '<div><nav class="text-center" aria-label="Page navigation"><ul class="pagination">';
				for(var a = 0; a < n; a++) {
					if(a == 0) {
						html += '<li class="active"><a href="#">' + (a + 1) + '</a></li>'
					} else {
						html += '<li><a href="#">' + (a + 1) + '</a></li>'
					}

				}
				html += '</ul></nav></div>';
			}

			$('#home').html(str + html);
			$('.pagination a').on('click', function(e) {
				console.log($(e.target).text());
				if($(e.target).text() == '下一页') {
					if($('.pagination li.active a').text() == n) {
						alert('已经到头了');
					} else if(parseInt($('.pagination li:nth-child(6) a').text()) == n) {
						$('.pagination li.active').next().addClass('active');
						$($('.pagination li.active')[1]).prev().removeClass('active');
					} else {
						$('.pagination li:nth-child(6) a').text(parseInt($('.pagination li:nth-child(6) a').text()) + 1);
						$('.pagination li:nth-child(5) a').text(parseInt($('.pagination li:nth-child(5) a').text()) + 1);
						$('.pagination li:nth-child(4) a').text(parseInt($('.pagination li:nth-child(4) a').text()) + 1);
						$('.pagination li:nth-child(3) a').text(parseInt($('.pagination li:nth-child(3) a').text()) + 1);
						$('.pagination li:nth-child(2) a').text(parseInt($('.pagination li:nth-child(2) a').text()) + 1);
					}
					$.ajax({
						url: url + '/service/findinviteroom',
						type: 'post',
						data: {
							id: msg.userid,
							limit: 10,
							page: $('.pagination li.active a').text()
						},
						headers: {
							'Authorization': 'Bearer ' + msg.token
						},
						success: function(d3) {
							console.log(d3);
							var str = '';
							var stdiv = '';
							for(var i = 0; i < d3.content.length; i++) {
								var str3 = '';
								if(parseInt(d3.content[i].results) == 1) {
									str3 = '<span style="color: green">已接受</span>';
								}
								if(parseInt(d3.content[i].results) == 2) {
									str3 = '<span style="color: red">已拒绝</span>';
								}
								if(parseInt(d3.content[i].results) == 3) {
									str3 = '<span style="color: red">已关闭</span>';
								}
								if(parseInt(d3.content[i].results) == 0) {
									str3 = '<button type="button" class="btn" ' +
										'style="background:#1abb9c;border: none;width: 86px;color: #fff;font-size: 14px"' +
										' roomid="' + d3.content[i].roomID + '" onclick="joinroom(event)" _id="' + d3.content[i]._id + '" roomname="'+d2.content[i].roomname+'" socketid="' + d3.content[i].socket_id + '">立即进入</button>' +
										' <button type="button" class="btn"' +
										'style="background:#fff;border: 1px solid #1abb9c;width: 86px;color: #1abb9c;font-size: 14px"' +
										' onclick="notjoin(event)" _id="' + d3.content[i]._id + '" socketid="' + d3.content[i].socket_id + '">拒绝</button> ';
								}
								stdiv += '<div class="row bg-color"><div class="col-md-6"><div class="leftmsg"> ' +
									'<p>客服' + d3.content[i].username + ' <span>邀请你加入小组聊天</span></p>' +
									' <div class="rightmsg">' + d3.content[i].time + '</div> ' +
									' </div>' +
									'</div>' +
									' <div class="col-md-6 text-center"> ' + str3 +
									'</div></div>';
							}
							str += stdiv;
							$('.row.bg-color').remove();
							$('.pagination').parent().parent().before(str);
						}
					});
				} else if($(e.target).text() == '上一页') {
					if($('.pagination li.active a').text() == 1) {
						alert('已经到头了');
					} else if(parseInt($('.pagination li:nth-child(2) a').text()) == 1) {
						$('.pagination li.active').prev().addClass('active');
						$($('.pagination li.active')[0]).next().removeClass('active');
					} else {
						$('.pagination li:nth-child(6) a').text(parseInt($('.pagination li:nth-child(6) a').text()) - 1);
						$('.pagination li:nth-child(5) a').text(parseInt($('.pagination li:nth-child(5) a').text()) - 1);
						$('.pagination li:nth-child(4) a').text(parseInt($('.pagination li:nth-child(4) a').text()) - 1);
						$('.pagination li:nth-child(3) a').text(parseInt($('.pagination li:nth-child(3) a').text()) - 1);
						$('.pagination li:nth-child(2) a').text(parseInt($('.pagination li:nth-child(2) a').text()) - 1);
					}
					$.ajax({
						url: url + '/service/findinviteroom',
						type: 'post',
						data: {
							id: msg.userid,
							limit: 10,
							page: $('.pagination li.active a').text()
						},
						headers: {
							'Authorization': 'Bearer ' + msg.token
						},
						success: function(d2) {
							console.log(d2);
							var str = '';
							var stdiv = '';
							for(var i = 0; i < d2.content.length; i++) {
								var str3 = '';
								if(parseInt(d2.content[i].results) == 1) {
									str3 = '<span style="color: green">已接受</span>';
								}
								if(parseInt(d2.content[i].results) == 2) {
									str3 = '<span style="color: red">已拒绝</span>';
								}
								if(parseInt(d2.content[i].results) == 3) {
									str3 = '<span style="color: red">已关闭</span>';
								}

								if(parseInt(d2.content[i].results) == 0) {
									str3 = '<button type="button" class="btn" ' +
										'style="background:#1abb9c;border: none;width: 86px;color: #fff;font-size: 14px"' +
										' roomid="' + d2.content[i].roomID + '" roomname="'+d2.content[i].roomname+'" onclick="joinroom(event)" _id="' + d2.content[i]._id + '" socketid="' + d2.content[i].socket_id + '">立即进入</button>' +
										' <button type="button" class="btn"' +
										'style="background:#fff;border: 1px solid #1abb9c;width: 86px;color: #1abb9c;font-size: 14px"' +
										' onclick="notjoin(event)" _id="' + d2.content[i]._id + '" socketid="' + d2.content[i].socket_id + '" >拒绝</button> ';
								}
								stdiv += '<div class="row bg-color"><div class="col-md-6"><div class="leftmsg"> ' +
									'<p>客服' + d2.content[i].username + ' <span>邀请你加入小组聊天</span></p>' +
									' <div class="rightmsg">' + d2.content[i].time + '</div> ' +
									' </div>' +
									'</div>' +
									' <div class="col-md-6 text-center"> ' + str3 +
									'</div></div>';
							}
							str += stdiv;
							$('.row.bg-color').remove();
							$('.pagination').parent().parent().before(str);
						}
					});

				} else {
					$(e.target).parent().addClass('active');
					$(e.target).parent().siblings().removeClass('active');
					$.ajax({
						url: url + '/service/findinviteroom',
						type: 'post',
						data: {
							id: msg.userid,
							limit: 10,
							page: $('.pagination li.active a').text()
						},
						headers: {
							'Authorization': 'Bearer ' + msg.token
						},
						success: function(d2) {
							console.log(d2);
							var str = '';
							var stdiv = '';
							for(var i = 0; i < d2.content.length; i++) {
								var str3 = '';
								if(parseInt(d2.content[i].results) == 1) {
									str3 = '<span style="color: green">已接受</span>';
								}
								if(parseInt(d2.content[i].results) == 2) {
									str3 = '<span style="color: red">已拒绝</span>';
								}
								if(parseInt(d2.content[i].results) == 3) {
									str3 = '<span style="color: red">已关闭</span>';
								}
								if(parseInt(d2.content[i].results) == 0) {
									str3 = '<button type="button" class="btn" ' +
										'style="background:#1abb9c;border: none;width: 86px;color: #fff;font-size: 14px"' +
										' roomid="' + d2.content[i].roomID + '" roomname="'+d2.content[i].roomname+'" onclick="joinroom(event)" _id="' + d2.content[i]._id + '" socketid="' + d2.content[i].socket_id + '">立即进入</button>' +
										' <button type="button" class="btn"' +
										'style="background:#fff;border: 1px solid #1abb9c;width: 86px;color: #1abb9c;font-size: 14px"' +
										' onclick="notjoin(event)" _id="' + d2.content[i]._id + '" socketid="' + d2.content[i].socket_id + '">拒绝</button> ';
								}
								stdiv += '<div class="row bg-color"><div class="col-md-6"><div class="leftmsg"> ' +
									'<p>客服' + d2.content[i].username + ' <span>邀请你加入小组聊天</span></p>' +
									' <div class="rightmsg">' + d2.content[i].time + '</div> ' +
									' </div>' +
									'</div>' +
									' <div class="col-md-6 text-center"> ' + str3 +
									'</div></div>';
							}
							str += stdiv;
							$('.row.bg-color').remove();
							$('.pagination').parent().parent().before(str);
						}
					});
				}
			})
			$.ajax({
				url: url + '/service/find_Roominfo',
				type: 'post',
				data: {
					userid: msg.userid
				},
				headers: {
					'Authorization': 'Bearer ' + msg.token
				},
				success: function(data) {
					console.log(data);
					var str = '';
					var str2 = '';
					var str3 = '';
					if(data.content.length>0){
                        for(var b = 0; b < data.content.length; b++) {
                            if(data.content[b] == mysocketid) {

                            } else {
                                if(b == 0) {
                                    str += '<li role="presentation" class="socketroom" roomid="' + data.content[b].roomID + '" roomname="'+data.content[b].roomname+'"><a  href="#' + data.content[b].roomID + '" aria-controls="' + data.content[b].roomID + '" role="tab" data-toggle="tab">' + data.content[b].roomname + ' <span class="badge2"></span></a></li>';
                                    str2 += '<div role="tabpanel" class="tab-pane" id="' + data.content[b].roomID + '"><div class="col-md-5 col-sm-5 col-lg-5 msgbox" roomid="' + data.content[b].roomID + '">' +
                                        '<div class="topmsg"></div>' +
                                        '<div class="txtmsg"><form id="' + roomid + '" enctype="multipart/form-data">' +
                                        '<a href="javascript:;" class="file"><input type="file" name="Filedata" id="" class="upload" roomid="' + data.content[b].roomID + '"></a></form><textarea class="message" placeholder="在此输入"></textarea></div>' +
                                        '<div class="text-right" style="height: 35px;background: #fff;"><button  type="button" class="over" roomname="'+data.content[b].roomname+'" roomid="' + data.content[b].roomID + '" onclick="leaveroom(event)">退出聊天</button>' +
                                        '<button  type="button" class="send" roomid="' + data.content[b].roomID + '" roomname="'+data.content[b].roomname+'" onclick="sendmsg(event)">发送</button> </div> </div><div class="col-md-5 col-sm-5 col-lg-5 msgbox msgboxdd">' +
                                        '' +
                                        '</div></div>';
                                } else {
                                    str += '<li role="presentation" class="socketroom" roomid="' + data.content[b].roomID + '" roomname="'+data.content[b].roomname+'"><a href="#' + data.content[b].roomID + '" aria-controls="' + data.content[b].roomID + '" role="tab" data-toggle="tab">' + data.content[b].roomID + ' <span class="badge2"></span></a></li>';
                                    str2 += '<div role="tabpanel" class="tab-pane" id="' + data.content[b].roomID + '"><div class="col-md-5 col-sm-5 col-lg-5 msgbox" roomid="' + data.content[b].roomID + '">' +
                                        '<div class="topmsg"></div>' +
                                        '<div class="txtmsg"><form id="' + roomid + '" enctype="multipart/form-data">' +
                                        '<a href="javascript:;" class="file"><input type="file" name="Filedata" class="upload" roomid="' + data.content[b].roomID + '"></a></form><textarea class="message" placeholder="在此输入"></textarea></div>' +
                                        '<div class="text-right" style="height: 35px;background: #fff;"><button  type="button" class="over" roomname="'+data.content[b].roomname+'" roomid="' + data.content[b].roomID + '"  onclick="leaveroom(event)">退出聊天</button>' +
                                        '<button  type="button" class="send" roomid="' + data.content[b].roomID + '" roomname="'+data.content[b].roomname+'" onclick="sendmsg(event)">发送</button> </div> ' +
                                        '</div><div class="col-md-5 col-sm-5 col-lg-5 msgbox msgboxdd">' +
                                        '' +
                                        '</div></div>';
                                }
                            }
                        }
					}else{
                        str = '<li class="text-center"><img src="../../img/empty-s.png" alt="" style="margin-top: 200px"></li>';
                        str2 += '<div><div class="col-md-5 col-sm-5 col-lg-5 msgbox">' +
                            '<div class="topmsg"></div>' +
                            '<div class="txtmsg"><form enctype="multipart/form-data">' +
                            '<a href="javascript:;" class="file"><input type="file" name="Filedata" id="" class="upload"></a></form><textarea placeholder="在此输入"></textarea></div>' +
                            '<div class="text-right" style="height: 35px;background: #fff;"><button  type="button" class="over" >退出聊天</button>' +
                            '<button  type="button" class="send"  >发送</button> </div> </div><div class="col-md-5 col-sm-5 col-lg-5 msgbox msgboxdd">' +
                            '' +
                            '</div></div>';
                    }

					$('.myroom').html(str);
					$('.roombox').html(str2);
					$('.msgbox').css('height', '600px');
					$('.socketroom a').on('click', function(e) {
						e.preventDefault();
						date_cc = '';
						var rid = $(e.target).attr('aria-controls');
						if(canemit) {
							socket.emit('joinroom', {
								roomID: $(e.target).attr('aria-controls'),
								username: msg.username,
								sex: 0,
								userrole: msg.userrole,
								token: msg.token

							});
						} else {
							canemit = false;
						}
						canemit = false;
						$.ajax({
							url: url + '/service/findchat ',
							type: 'post',
							data: {
								roomID: $(e.target).attr('aria-controls'),
								username: msg.username, //用户名
								limit: 20,
								page: 1
							},
							headers: {
								'Authorization': 'Bearer ' + msg.token
							},
							success: function(da) {
								console.log(da);
								$('[roomid=' + $(e.target).attr('aria-controls') + '] .topmsg').html('');
								if(da.data.length > 0) {
									for(var c = 0; c < da.data.length; c++) {
										var imgmsg = (da.data[c].imggs == null || da.data[c].imggs == '') ? '' : '<img src="' + da.data[c].imggs + '" width="100%" alt="">';
										var datestr = '';
										if(c > 0) {
											if(da.data[c].senddate.split(",")[0] != da.data[c - 1].senddate.split(",")[0]) {
												datestr = da.data[c - 1].senddate.split(",")[0];
												$('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="text-center">' + datestr + '</div>');
											}
										} else {
											datestr = date_cc;
											$('div[date-show="' + da.data[c].senddate.split(',')[0] + '"]').length == 0 ?
												$('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="text-center">' + datestr + '</div>') : '';
										}
                                        if(da.data[c].userRole == 1||da.data[c].userRole == 6) {
                                            $('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="media" date-show="' + da.data[c].senddate.split(',')[0] + '">' +
                                                '<div class="media-body"><h5 class="media-heading text-right">' + da.data[c].username + '&nbsp;&nbsp;' + da.data[c].senddate.split(',')[1] + '</h5>' +
                                                '<div style="float: right;background: #1abb9c;color: #fff" class="msgdetail">' +
                                                imgmsg + da.data[c].msg +
                                                '</div></div><div class="media-right media-top"><a href="#">' +
                                                '<img class="media-object img-circle" src="' + da.data[c].anonymityImg + '" width="40" height="40" alt="..."></a></div></div>');
                                        } else if(da.data[c].userRole == 3) {
                                            $('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="media" date-show="' + da.data[c].senddate.split(',')[0] + '"><div class="media-left media-top"><a href="#">' +
                                                '<img class="media-object img-circle" src="' + da.data[c].anonymityImg + '" width="40" height="40" alt="..."></a></div>' +
                                                '<div class="media-body"><h5 class="media-heading">' + da.data[c].username + '&nbsp;&nbsp;' + da.data[c].senddate.split(',')[1] + '</h5>' +
                                                '<div style="float: left;background: #59bdda;color: #fff" class="msgdetail">' +
                                                imgmsg + da.data[c].msg + '</div></div></div>');

                                        }else {
                                            $('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="media" date-show="' + da.data[c].senddate.split(',')[0] + '"><div class="media-left media-top"><a href="#">' +
                                                '<img class="media-object img-circle" src="' + da.data[c].anonymityImg + '" width="40" height="40" alt="..."></a></div>' +
                                                '<div class="media-body"><h5 class="media-heading">' + da.data[c].username + '&nbsp;&nbsp;' + da.data[c].senddate.split(',')[1] + '</h5>' +
                                                '<div style="float: left;background: #fff;" class="msgdetail">' +
                                                imgmsg + da.data[c].msg + '</div></div></div>');

                                        }

									}
									date_cc = da.data[da.data.length - 1].senddate.split(",")[0];
								}
								$('[roomid=' + $(e.target).attr('aria-controls') + '] .topmsg').prepend('<div class="text-center"><a class="btn findmore" onclick="findmore(event)" data-page="1">查看更多</a></div>')
								var div = document.getElementById($(e.target).attr('aria-controls')).firstElementChild.firstElementChild;
								console.log(div);
								div.scrollTop = div.scrollHeight;
								$('.topmsg img').on('click', function(e) {
									console.log($(this).attr('src'));
									stopPropagation(e);
									$('.Reading_pictures_show').html('');
									$('.Reading_pictures_show').append('<img src="' + $(this).attr('src') + '" width="100%">');
									$('.Reading_pictures_show').css('display', 'block');
								})
							}
						});
					})
					$('.upload').on('change', function() {
						var me = this;
						console.log($(this));
						if(this.files) {
							//读取图片数据
							var f = this.files[0];
							var reader = new FileReader();
							reader.onload = function(e) {
								var data = e.target.result;
								//加载图片获取图片真实宽度和高度
								var image = new Image();
								image.onload = function() {
									var width = image.width;
									var height = image.height;
									console.log(width + '======' + height + "=====" + f.size);
									if(f.size < (5 * 1024 * 1024)) {
										var rid = $(me).attr('roomid');
										var formData = new FormData();
										formData.append('files', $(me)[0].files[0]);
										$.ajax({
											url: "https://xcx.juseyx.cn/service/uploadimg",
											type: "post",
											cache: false,
											data: formData,
											processData: false,
											contentType: false,
											success: function(res) {
												console.log(res);
												if(res.code == 200) {
													socket.emit('message', {
														msg: '', //发送内容 String
														roomID: rid, //房间名String
														username: msg.username, //发送者id String
														type: 'msg', //消息类型 String
														b: 1,
														anonymityImg: "https://xcximg.juseyun.com/Images/doctor.png",
														headerimg: null,
														color: false,
														to: null,
														imggs: res.data,
														id: 0,
														is: 2,
														token: msg.token,
                                                        userRole:msg.userrole
													});

												}
											}
										})
									} else {
										alert('图片过大请重新选择')
									}
								};
								image.src = data;
							};
							reader.readAsDataURL(f);
						} else {
							var image = new Image();
							image.onload = function() {
								var width = image.width;
								var height = image.height;
								var fileSize = image.fileSize;
								alert(width + '======' + height + "=====" + fileSize);
							}
							image.src = this.value;
						}

					});
					$("textarea").keydown(function(e) {
						var roomid = $(e.target).parent().next().children().attr('roomid');
						if(e.keyCode == 13) {
							e.returnValue = $.trim($('[roomid=' + roomid + '] .message').val());
							e.preventDefault();
							if(e.returnValue == "") {
								alert('输入不能为空,请重新输入');
							} else {
								if($('[roomid=' + roomid + '] .message').val().length > 500) {
									alert('发送消息内容超长，请分条发送。');
								} else {

									socket.emit('message', {
										msg: $('[roomid=' + roomid + '] .message').val(), //发送内容 String
										roomID: roomid, //房间名String
										username: msg.username, //发送者id String
										type: 'msg', //消息类型 String
										b: 1,
										anonymityImg: "https://xcximg.juseyun.com/Images/doctor.png",
										headerimg: null,
										color: false,
										to: null,
										imggs: "",
										id: 0,
										is: 2,
										token: msg.token,
                                        userRole:msg.userrole
									});
									$('[roomid=' + roomid + '] .message').val('');
									$('[roomid=' + roomid + '] .message').focus();

								}

							}
						}
					});
				}
			})
		}
	});
	socket.emit('login', {
		token: msg.token,
		username: msg.username,
		openid: null,
		sex: 0,
		marking: 1, //1pc  2wx  3app  Number
		userrole: msg.userrole //登陆角色 (医生(1)，用户(2)，助理(3)，药师(4),匿名用户(5))
	});
	socket.on('inviteroom', function(data) {
		console.log(data);
		window.top.document.getElementsByClassName('invitelist')[0].innerHTML =
			parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) + 1;
		window.top.document.getElementsByClassName('invitelist')[0].style.display = 'inline-block';
		$.ajax({
			url: url + '/service/inviteroom',
			type: 'post',
			data: {
				id: msg.userid,
				roomID: data.roomID,
				time: data.time,
				username: data.username,
				state: 0,
				socket_id: data.socket_id,
				roomname:data.roomname
			},
			headers: {
				'Authorization': 'Bearer ' + msg.token
			},
			success: function(d) {
				$.ajax({
					url: url + '/service/findinviteroom',
					type: 'post',
					data: {
						id: msg.userid,
						limit: 10,
						page: 1
					},
					headers: {
						'Authorization': 'Bearer ' + msg.token
					},
					success: function(d2) {
						console.log(d2);
						var str = '';
                        if(d2.content.length==0){
                            str='<p class="text-center"><img src="../../img/empty-b.png" alt="" style="margin-top: 200px"></p>'
                        }
						$('#home').html(str);
						var stdiv = '';
						for(var i = 0; i < d2.content.length; i++) {
							var str3 = '';
							if(parseInt(d2.content[i].results) == 1) {
								str3 = '<span style="color: green">已接受</span>';
							}
							if(parseInt(d2.content[i].results) == 2) {
								str3 = '<span style="color: red">已拒绝</span>';
							}
							if(parseInt(d2.content[i].results) == 3) {
								str3 = '<span style="color: red">已关闭</span>';
							}
							if(parseInt(d2.content[i].results) == 0) {
								str3 = '<button type="button" class="btn" ' +
									'style="background:#1abb9c;border: none;width: 86px;color: #fff;font-size: 14px"' +
									' roomid="' + d2.content[i].roomID + '" onclick="joinroom(event)" _id="' + d2.content[i]._id + '" socketid="' + d2.content[i].socket_id + '" roomname="' + d2.content[i].roomname + '">立即进入</button>' +
									' <button type="button" class="btn"' +
									'style="background:#fff;border: 1px solid #1abb9c;width: 86px;color: #1abb9c;font-size: 14px"' +
									' onclick="notjoin(event)" _id="' + d2.content[i]._id + '" socketid="' + d2.content[i].socket_id + '"  roomname="' + d2.content[i].roomname + '">拒绝</button> ';
							}
							stdiv += '<div class="row bg-color"><div class="col-md-6"><div class="leftmsg"> ' +
								'<p>客服' + d2.content[i].username + ' <span>邀请你加入小组聊天</span></p>' +
								' <div class="rightmsg">' + d2.content[i].time + '</div> ' +
								' </div>' +
								'</div>' +
								' <div class="col-md-6 text-center"> ' + str3 +
								'</div></div>';
						}
						str += stdiv;
						var n = Math.ceil(d2.count / 10);
						if(n > 5) {
							var html = '<div><nav class="text-center" aria-label="Page navigation"><ul class="pagination"><li><a href="#" aria-label="Previous"><span aria-hidden="true">上一页</span></a></li>';
							html += '<li class="active"><a href="#">1</a></li>' +
								'<li><a href="#">2</a></li><li><a href="#">3</a></li>' +
								'<li><a href="#">4</a></li><li><a href="#">5</a></li>';
							html += '<li><a href="#" aria-label="Next"><span aria-hidden="true">下一页</span></a></li> </ul></nav></div>';
						} else {
							var html = '<div><nav class="text-center" aria-label="Page navigation"><ul class="pagination">';
							for(var a = 0; a < n; a++) {
								if(a == 0) {
									html += '<li class="active"><a href="#">' + (a + 1) + '</a></li>'
								} else {
									html += '<li><a href="#">' + (a + 1) + '</a></li>'
								}

							}
							html += '</ul></nav></div>';
						}

						$('#home').html(str + html);
						$('.pagination a').on('click', function(e) {
							console.log($(e.target).text());
							if($(e.target).text() == '下一页') {
								if($('.pagination li.active a').text() == n) {
									alert('已经到头了');
								} else if(parseInt($('.pagination li:nth-child(6) a').text()) == n) {
									$('.pagination li.active').next().addClass('active');
									$($('.pagination li.active')[1]).prev().removeClass('active');
								} else {
									$('.pagination li:nth-child(6) a').text(parseInt($('.pagination li:nth-child(6) a').text()) + 1);
									$('.pagination li:nth-child(5) a').text(parseInt($('.pagination li:nth-child(5) a').text()) + 1);
									$('.pagination li:nth-child(4) a').text(parseInt($('.pagination li:nth-child(4) a').text()) + 1);
									$('.pagination li:nth-child(3) a').text(parseInt($('.pagination li:nth-child(3) a').text()) + 1);
									$('.pagination li:nth-child(2) a').text(parseInt($('.pagination li:nth-child(2) a').text()) + 1);
								}
								$.ajax({
									url: url + '/service/findinviteroom',
									type: 'post',
									data: {
										id: msg.userid,
										limit: 10,
										page: $('.pagination li.active a').text()
									},
									headers: {
										'Authorization': 'Bearer ' + msg.token
									},
									success: function(d3) {
										console.log(d3);
										var str = '';
										var stdiv = '';
										for(var i = 0; i < d3.content.length; i++) {
											var str3 = '';
											if(parseInt(d3.content[i].results) == 1) {
												str3 = '<span style="color: green">已接受</span>';
											}
											if(parseInt(d3.content[i].results) == 2) {
												str3 = '<span style="color: red">已拒绝</span>';
											}
											if(parseInt(d3.content[i].results) == 3) {
												str3 = '<span style="color: red">已关闭</span>';
											}
											if(parseInt(d3.content[i].results) == 0) {
												str3 = '<button type="button" class="btn" ' +
													'style="background:#1abb9c;border: none;width: 86px;color: #fff;font-size: 14px"' +
													' roomid="' + d3.content[i].roomID + '" roomname="'+d3.content[i].roomname+'" onclick="joinroom(event)" _id="' + d3.content[i]._id + '" socketid="' + d3.content[i].socket_id + '">立即进入</button>' +
													' <button type="button" class="btn"' +
													'style="background:#fff;border: 1px solid #1abb9c;width: 86px;color: #1abb9c;font-size: 14px"' +
													' onclick="notjoin(event)" _id="' + d3.content[i]._id + '" socketid="' + d3.content[i].socket_id + '">拒绝</button> ';
											}
											stdiv += '<div class="row bg-color"><div class="col-md-6"><div class="leftmsg"> ' +
												'<p>客服' + d3.content[i].username + ' <span>邀请你加入小组聊天</span></p>' +
												' <div class="rightmsg">' + d3.content[i].time + '</div> ' +
												' </div>' +
												'</div>' +
												' <div class="col-md-6 text-center"> ' + str3 +
												'</div></div>';
										}
										str += stdiv;
										$('.row.bg-color').remove();
										$('.pagination').parent().parent().before(str);
									}
								});
							} else if($(e.target).text() == '上一页') {
								if($('.pagination li.active a').text() == 1) {
									alert('已经到头了');
								} else if(parseInt($('.pagination li:nth-child(2) a').text()) == 1) {
									$('.pagination li.active').prev().addClass('active');
									$($('.pagination li.active')[0]).next().removeClass('active');
								} else {
									$('.pagination li:nth-child(6) a').text(parseInt($('.pagination li:nth-child(6) a').text()) - 1);
									$('.pagination li:nth-child(5) a').text(parseInt($('.pagination li:nth-child(5) a').text()) - 1);
									$('.pagination li:nth-child(4) a').text(parseInt($('.pagination li:nth-child(4) a').text()) - 1);
									$('.pagination li:nth-child(3) a').text(parseInt($('.pagination li:nth-child(3) a').text()) - 1);
									$('.pagination li:nth-child(2) a').text(parseInt($('.pagination li:nth-child(2) a').text()) - 1);
								}
								$.ajax({
									url: url + '/service/findinviteroom',
									type: 'post',
									data: {
										id: msg.userid,
										limit: 10,
										page: $('.pagination li.active a').text()
									},
									headers: {
										'Authorization': 'Bearer ' + msg.token
									},
									success: function(d2) {
										console.log(d2);
										var str = '';
										var stdiv = '';
										for(var i = 0; i < d2.content.length; i++) {
											var str3 = '';
											if(parseInt(d2.content[i].results) == 1) {
												str3 = '<span style="color: green">已接受</span>';
											}
											if(parseInt(d2.content[i].results) == 2) {
												str3 = '<span style="color: red">已拒绝</span>';
											}
											if(parseInt(d2.content[i].results) == 3) {
												str3 = '<span style="color: red">已关闭</span>';
											}
											if(parseInt(d2.content[i].results) == 0) {
												str3 = '<button type="button" class="btn" ' +
													'style="background:#1abb9c;border: none;width: 86px;color: #fff;font-size: 14px"' +
													' roomid="' + d2.content[i].roomID + '" roomname="'+d2.content[i].roomname+'" onclick="joinroom(event)" _id="' + d2.content[i]._id + '" socketid="' + d2.content[i].socket_id + '">立即进入</button>' +
													' <button type="button" class="btn"' +
													'style="background:#fff;border: 1px solid #1abb9c;width: 86px;color: #1abb9c;font-size: 14px"' +
													' onclick="notjoin(event)" _id="' + d2.content[i]._id + '" socketid="' + d2.content[i].socket_id + '">拒绝</button> ';
											}
											stdiv += '<div class="row bg-color"><div class="col-md-6"><div class="leftmsg"> ' +
												'<p>客服' + d2.content[i].username + ' <span>邀请你加入小组聊天</span></p>' +
												' <div class="rightmsg">' + d2.content[i].time + '</div> ' +
												' </div>' +
												'</div>' +
												' <div class="col-md-6 text-center"> ' + str3 +
												'</div></div>';
										}
										str += stdiv;
										$('.row.bg-color').remove();
										$('.pagination').parent().parent().before(str);
									}
								});

							} else {
								$(e.target).parent().addClass('active');
								$(e.target).parent().siblings().removeClass('active');
								$.ajax({
									url: url + '/service/findinviteroom',
									type: 'post',
									data: {
										id: msg.userid,
										limit: 10,
										page: $('.pagination li.active a').text()
									},
									headers: {
										'Authorization': 'Bearer ' + msg.token
									},
									success: function(d2) {
										console.log(d2);
										var str = '';
										var stdiv = '';
										for(var i = 0; i < d2.content.length; i++) {
											var str3 = '';
											if(parseInt(d2.content[i].results) == 1) {
												str3 = '<span style="color: green">已接受</span>';
											}
											if(parseInt(d2.content[i].results) == 2) {
												str3 = '<span style="color: red">已拒绝</span>';
											}
											if(parseInt(d2.content[i].results) == 3) {
												str3 = '<span style="color: red">已关闭</span>';
											}
											if(parseInt(d2.content[i].results) == 0) {
												str3 = '<button type="button" class="btn" ' +
													'style="background:#1abb9c;border: none;width: 86px;color: #fff;font-size: 14px"' +
													' roomid="' + d2.content[i].roomID + '" roomname="'+d2.content[i].roomname+'" onclick="joinroom(event)" _id="' + d2.content[i]._id + '" socketid="' + d2.content[i].socket_id + '">立即进入</button>' +
													' <button type="button" class="btn"' +
													'style="background:#fff;border: 1px solid #1abb9c;width: 86px;color: #1abb9c;font-size: 14px"' +
													' onclick="notjoin(event)" _id="' + d2.content[i]._id + '" socketid="' + d2.content[i].socket_id + '">拒绝</button> ';
											}
											stdiv += '<div class="row bg-color"><div class="col-md-6"><div class="leftmsg"> ' +
												'<p>客服' + d2.content[i].username + ' <span>邀请你加入小组聊天</span></p>' +
												' <div class="rightmsg">' + d2.content[i].time + '</div> ' +
												' </div>' +
												'</div>' +
												' <div class="col-md-6 text-center"> ' + str3 +
												'</div></div>';
										}
										str += stdiv;
										$('.row.bg-color').remove();
										$('.pagination').parent().parent().before(str);
									}
								});
							}
						})
						$.ajax({
							url: url + '/service/find_Roominfo',
							type: 'post',
							data: {
								userid: msg.userid
							},
							headers: {
								'Authorization': 'Bearer ' + msg.token
							},
							success: function(data) {
								console.log(data);
								var str = '';
								var str2 = '';
								var str3 = '';
                                if(data.content.length>0){
                                    for(var b = 0; b < data.content.length; b++) {
                                        if(data.content[b] == mysocketid) {

                                        } else {
                                            if(b == 0) {
                                                str += '<li role="presentation" class="socketroom" roomid="' + data.content[b].roomID + '" roomname="'+data.content[b].roomname+'"><a  href="#' + data.content[b].roomID+ '" aria-controls="' + data.content[b].roomID + '" role="tab" data-toggle="tab">' + data.content[b].roomname + ' <span class="badge2"></span></a></li>';
                                                str2 += '<div role="tabpanel" class="tab-pane" id="' + data.content[b].roomID + '"><div class="col-md-5 col-sm-5 col-lg-5 msgbox" roomid="' + data.content[b].roomID + '">' +
                                                    '<div class="topmsg"></div>' +
                                                    '<div class="txtmsg"><form id="' + roomid + '" enctype="multipart/form-data">' +
                                                    '<a href="javascript:;" class="file"><input type="file" name="Filedata" id="" class="upload" roomid="' + data.content[b].roomID + '"></a></form><textarea class="message" placeholder="在此输入"></textarea></div>' +
                                                    '<div class="text-right" style="height: 35px;background: #fff;"><button  type="button" class="over" roomid="' + data.content[b].roomID + '" roomname="'+data.content[b].roomname+'" onclick="leaveroom(event)">退出聊天</button>' +
                                                    '<button  type="button" class="send" roomid="' + data.content[b].roomID + '" onclick="sendmsg(event)">发送</button> </div> </div><div class="col-md-5 col-sm-5 col-lg-5 msgbox msgboxdd">' +
                                                    '' +
                                                    '</div></div>';
                                            } else {
                                                str += '<li role="presentation" class="socketroom" roomid="' + data.content[b].roomID + '" roomname="'+data.content[b].roomname+'"><a href="#' + data.content[b].roomID + '" aria-controls="' + data.content[b].roomID + '" role="tab" data-toggle="tab">' + data.content[b].roomID + ' <span class="badge2"></span></a></li>';
                                                str2 += '<div role="tabpanel" class="tab-pane" id="' + data.content[b].roomID + '"><div class="col-md-5 col-sm-5 col-lg-5 msgbox" roomid="' + data.content[b].roomID + '">' +
                                                    '<div class="topmsg"></div>' +
                                                    '<div class="txtmsg"><form id="' + roomid + '" enctype="multipart/form-data">' +
                                                    '<a href="javascript:;" class="file"><input type="file" name="Filedata" class="upload" roomid="' + data.content[b].roomID + '"></a></form><textarea class="message" placeholder="在此输入"></textarea></div>' +
                                                    '<div class="text-right" style="height: 35px;background: #fff;"><button  type="button" class="over" roomname="'+data.content[b].roomname+'" roomid="' + data.content[b].roomID+ '" onclick="leaveroom(event)">退出聊天</button>' +
                                                    '<button  type="button" class="send" roomid="' + data.content[b].roomID + '" onclick="sendmsg(event)">发送</button> </div> ' +
                                                    '</div><div class="col-md-5 col-sm-5 col-lg-5 msgbox msgboxdd">' +
                                                    '' +
                                                    '</div></div>';
                                            }
                                        }
                                    }
                                }else{
                                    str = '<li class="text-center"><img src="../../img/empty-s.png" alt="" style="margin-top: 200px"></li>';
                                    str2 += '<div><div class="col-md-5 col-sm-5 col-lg-5 msgbox">' +
                                        '<div class="topmsg"></div>' +
                                        '<div class="txtmsg"><form enctype="multipart/form-data">' +
                                        '<a href="javascript:;" class="file"><input type="file" name="Filedata" id="" class="upload"></a></form><textarea placeholder="在此输入"></textarea></div>' +
                                        '<div class="text-right" style="height: 35px;background: #fff;"><button  type="button" class="over" >退出聊天</button>' +
                                        '<button  type="button" class="send"  >发送</button> </div> </div><div class="col-md-5 col-sm-5 col-lg-5 msgbox msgboxdd">' +
                                        '' +
                                        '</div></div>';
                                }


                                $('.myroom').html(str);
								$('.roombox').html(str2);
								$('.msgbox').css('height', '600px');
								$('.socketroom a').on('click', function(e) {
									e.preventDefault();
									var rid = $(e.target).attr('aria-controls');
									if(canemit) {
										socket.emit('joinroom', {
											roomID: $(e.target).attr('aria-controls'),
											username: msg.username,
											sex: 0,
											userrole: msg.userrole,
											token: msg.token

										});
									} else {
										canemit = false;
									}
									canemit = false;
									$.ajax({
										url: url + '/service/findchat ',
										type: 'post',
										data: {
											roomID: $(e.target).attr('aria-controls'),
											username: msg.username, //用户名
											limit: 20,
											page: 1
										},
										headers: {
											'Authorization': 'Bearer ' + msg.token
										},
										success: function(da) {
											console.log(da);
											$('[roomid=' + $(e.target).attr('aria-controls') + '] .topmsg').html('');
											for(var c = 0; c < da.data.length; c++) {
												var imgmsg = (da.data[c].imggs == null || da.data[c].imggs == '') ? '' : '<img src="' + da.data[c].imggs + '" width="100%" alt="">';
												var datestr = '';
												if(c > 0) {
													if(da.data[c].senddate.split(",")[0] != da.data[c - 1].senddate.split(",")[0]) {
														datestr = da.data[c - 1].senddate.split(",")[0];
														$('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="text-center">' + datestr + '</div>');
													}
												} else {
													datestr = date_cc;
													$('div[date-show="' + da.data[c].senddate.split(',')[0] + '"]').length == 0 ?
														$('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="text-center">' + datestr + '</div>') : '';
												}
                                                if(da.data[c].userRole == 1||da.data[c].userRole == 6) {
                                                    $('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="media" date-show="' + da.data[c].senddate.split(',')[0] + '">' +
                                                        '<div class="media-body"><h5 class="media-heading text-right">' + da.data[c].username + '&nbsp;&nbsp;' + da.data[c].senddate.split(',')[1] + '</h5>' +
                                                        '<div style="float: right;background: #1abb9c;color: #fff" class="msgdetail">' +
                                                        imgmsg + da.data[c].msg +
                                                        '</div></div><div class="media-right media-top"><a href="#">' +
                                                        '<img class="media-object img-circle" src="' + da.data[c].anonymityImg + '" width="40" height="40" alt="..."></a></div></div>');
                                                } else if(da.data[c].userRole == 3) {
                                                    $('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="media" date-show="' + da.data[c].senddate.split(',')[0] + '"><div class="media-left media-top"><a href="#">' +
                                                        '<img class="media-object img-circle" src="' + da.data[c].anonymityImg + '" width="40" height="40" alt="..."></a></div>' +
                                                        '<div class="media-body"><h5 class="media-heading">' + da.data[c].username + '&nbsp;&nbsp;' + da.data[c].senddate.split(',')[1] + '</h5>' +
                                                        '<div style="float: left;background: #59bdda;color: #fff" class="msgdetail">' +
                                                        imgmsg + da.data[c].msg + '</div></div></div>');

                                                }else {
                                                    $('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="media" date-show="' + da.data[c].senddate.split(',')[0] + '"><div class="media-left media-top"><a href="#">' +
                                                        '<img class="media-object img-circle" src="' + da.data[c].anonymityImg + '" width="40" height="40" alt="..."></a></div>' +
                                                        '<div class="media-body"><h5 class="media-heading">' + da.data[c].username + '&nbsp;&nbsp;' + da.data[c].senddate.split(',')[1] + '</h5>' +
                                                        '<div style="float: left;background: #fff;" class="msgdetail">' +
                                                        imgmsg + da.data[c].msg + '</div></div></div>');

                                                }

											}
											date_cc = da.data[da.data.length - 1].senddate.split(",")[0];
											$('[roomid=' + $(e.target).attr('aria-controls') + '] .topmsg').prepend('<div class="text-center"><a class="btn findmore" onclick="findmore(event)" data-page="1">查看更多</a></div>')
											var div = document.getElementById($(e.target).attr('aria-controls')).firstElementChild.firstElementChild;
											console.log(div);
											div.scrollTop = div.scrollHeight;
											$('.topmsg img').on('click', function(e) {
												console.log($(this).attr('src'));
												stopPropagation(e);
												$('.Reading_pictures_show').html('');
												$('.Reading_pictures_show').append('<img src="' + $(this).attr('src') + '" width="100%">');
												$('.Reading_pictures_show').css('display', 'block');
											})
										}
									});
								})
								$('.upload').on('change', function() {
									var me = this;
									console.log($(this));
									if(this.files) {
										//读取图片数据
										var f = this.files[0];
										var reader = new FileReader();
										reader.onload = function(e) {
											var data = e.target.result;
											//加载图片获取图片真实宽度和高度
											var image = new Image();
											image.onload = function() {
												var width = image.width;
												var height = image.height;
												console.log(width + '======' + height + "=====" + f.size);
												if(f.size < (5 * 1024 * 1024)) {
													var rid = $(me).attr('roomid');
													var formData = new FormData();
													formData.append('files', $(me)[0].files[0]);
													$.ajax({
														url: "https://xcx.juseyx.cn/service/uploadimg",
														type: "post",
														cache: false,
														data: formData,
														processData: false,
														contentType: false,
														success: function(res) {
															console.log(res);
															if(res.code == 200) {

																socket.emit('message', {
																	msg: '', //发送内容 String
																	roomID: rid, //房间名String
																	username: msg.username, //发送者id String
																	type: 'msg', //消息类型 String
																	b: 1,
																	anonymityImg: "https://xcximg.juseyun.com/Images/doctor.png",
																	headerimg: null,
																	color: false,
																	to: null,
																	imggs: res.data,
																	id: 0,
																	is: 2,
																	token: msg.token,
                                                                    userRole:msg.userrole
																});

															}
														}
													})
												} else {
													alert('图片过大请重新选择')
												}
											};
											image.src = data;
										};
										reader.readAsDataURL(f);
									} else {
										var image = new Image();
										image.onload = function() {
											var width = image.width;
											var height = image.height;
											var fileSize = image.fileSize;
											alert(width + '======' + height + "=====" + fileSize);
										}
										image.src = this.value;
									}

								});
								$("textarea").keydown(function(e) {
									var roomid = $(e.target).parent().next().children().attr('roomid');
									if(e.keyCode == 13) {
										e.returnValue = $.trim($('[roomid=' + roomid + '] .message').val());
										e.preventDefault();
										if(e.returnValue == "") {
											alert('输入不能为空,请重新输入');
										} else {
											if($('[roomid=' + roomid + '] .message').val().length > 500) {
												alert('发送消息内容超长，请分条发送。');
											} else {
												socket.emit('message', {
													msg: $('[roomid=' + roomid + '] .message').val(), //发送内容 String
													roomID: roomid, //房间名String
													username: msg.username, //发送者id String
													type: 'msg', //消息类型 String
													b: 1,
													anonymityImg: "https://xcximg.juseyun.com/Images/doctor.png",
													headerimg: null,
													color: false,
													to: null,
													imggs: "",
													id: 0,
													is: 2,
													token: msg.token,
                                                    userRole:msg.userrole
												});
												$('[roomid=' + roomid + '] .message').val('');
												$('[roomid=' + roomid + '] .message').focus();

											}

										}
									}
								});
							}
						})
					}
				});
			}
		})
		// var now = new Date();
		// var y = now.getFullYear();
		// var m = now.getMonth() + 1;
		// var d = now.getDate();
		// var h = now.getHours();
		// h < 10 ? (h = '0' + h) : (h = h);
		// var min = now.getMinutes();
		// min < 10 ? (min = '0' + min) : (min = min);
		// var sec = now.getSeconds();
		// sec < 10 ? (sec = '0' + sec) : (sec = sec);
		// var nowtime = y + '-' + m + '-' + d + ' ' + h + ':' + min + ':' + sec;
		// data.time = nowtime;
		// console.log(data);
		// if(invitList.length > 0) {
		// 	for(var i = 0; i < invitList.length; i++) {
		// 		if(invitList[i].username == data.username && invitList[i].roomID == data.roomID) {
		// 			invitList[i] = data;
		// 		} else {
		// 			invitList.push(data);
		// 		}
		// 	}
		// } else {
		// 	invitList.push(data);
		// }

	});
	// for(var i=0;i<list.length;i++){
	//     var stdiv='';
	//     if(list[i].status==0){
	//         stdiv+='<div class="row bg-color"><div class="col-md-6"><div class="leftmsg"> ' +
	//             '<p>'+list[i].name+'</p> <p>邀请你加入小组聊天</p>' +
	//             ' </div>' +
	//             ' <div class="rightmsg">.'+list[i].time+'</div> ' +
	//             '</div>' +
	//             ' <div class="col-md-6 text-center"> ' +
	//             '<button type="button" class="btn btn-success">立即进入</button>' +
	//             ' <button type="button" class="btn btn-default">拒绝</button> ' +
	//             '</div></div>'
	//     }else{
	//         stdiv+='<div class="row bg-color"><div class="col-md-6"><div class="leftmsg"> ' +
	//             '<p>'+list[i].name+'<p>邀请你加入小组聊天</p>' +
	//             ' </div>' +
	//             ' <div class="rightmsg">.'+list[i].time+'</div> ' +
	//             '</div>' +
	//             ' <div class="col-md-6 text-center" style="color: red"> 已拒绝' +
	//             '</div></div>'
	//     }
	//     str+=stdiv;
	// }
	// str+='<div class="row fix-bottom">共 <span class="listtotal">'+list.length+'</span> 条记录</div>';

});
socket.on('inviteroomss', function(data) {
	console.log(data);
	if(data.content == false) {
		$.ajax({
			url: url + '/service/ficationinvite ',
			type: 'post',
			data: {
				id: msg.userid,
				_id: $('button[roomid="' + data.roomID + '"]').attr('_id'),
				state: 3
			},
			headers: {
				'Authorization': 'Bearer ' + msg.token
			},
			success: function(d) {
				$('button[roomid="' + data.roomID + '"]').parent().html('<span style="color: red">已关闭</span>');
				window.top.document.getElementsByClassName('invitelist')[0].innerHTML =
					parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) - 1;
				if(parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) > 0) {
					window.top.document.getElementsByClassName('invitelist')[0].style.display = 'inline-block';
				} else {
					window.top.document.getElementsByClassName('invitelist')[0].style.display = 'none';
				}
			}
		})
	}
});
socket.on('private_msg', function(data) {
	console.log(data);
	window.top.document.getElementsByClassName('unfinish')[0].innerHTML =
		parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) + 1;
	window.top.document.getElementsByClassName('unfinish')[0].style.display = 'inline-block';
})
socket.on('assigndoctor', function(data) {
	console.log(data);
	$.ajax({
		url: url + '/server/assDocPending',
		type: 'post',
		data: {
			id: msg.userid,
			userRole: msg.userrole
		},
		headers: {
			'Authorization': 'Bearer ' + msg.token
		},
		success: function(data) {
			console.log(data);
			if(data.code == 200) {
				window.top.document.getElementsByClassName('unfinish')[0].innerHTML = data.data.length;
				// $('.unfinish').text(data.data.length)
				if(data.data.length > 0) {
					window.top.document.getElementsByClassName('unfinish')[0].style.display = 'inline-block';
				} else {
					window.top.document.getElementsByClassName('unfinish')[0].style.display = 'none';
				}
			} else {
				alert(data.mes)
			}
		}
	});
	$('#' + data.content.roomID + ' .msgboxdd').html('<iframe  style="border:0; width:100%;height:788px;background-color:#FFF;"name="iframe" frameborder="0" ' +
		'src="../../postpaper/postpaper.html?detailId=' + data.content._id + '&&status=0">  </iframe>')
});
socket.on('userlogin', function(data) {
	console.log(data);
});

socket.on('disconnect', function(data) {
	console.log(data);
});
socket.on("sys", function(user, id, events) {
	console.log(id, events);
	console.log(user + "加入了" + id);
	if(events && events.events == 'out') {
		socket.emit('leave', {
			roomID: events.roomID,
			username: msg.username

		});
		$('#' + events.roomID + ' .topmsg').append('<div class="text-center" style="color:red;">会话已结束</div>');
	}
})
socket.on('msg', function(data, msg) {
	console.log(data, msg);
	var now = new Date();

	var h = now.getHours();
	h < 10 ? (h = '0' + h) : (h = h);
	var min = now.getMinutes();
	min < 10 ? (min = '0' + min) : (min = min);
	var sec = now.getSeconds();
	sec < 10 ? (sec = '0' + sec) : (sec = sec);
	var nowtime = h + ':' + min + ':' + sec;
	var imgmsg = (msg.imggs == null || msg.imggs == '') ? '' : '<img src="' + msg.imggs + '" width="100%" alt="">'
    if(msg.userRole == 1||msg.userRole == 6) {
        $('[roomid=' + msg.roomID + '] .topmsg').append('<div class="media" date-show="' + msg.senddate.split(',')[0] + '">' +
            '<div class="media-body"><h5 class="media-heading text-right">' + msg.username + '&nbsp;&nbsp;' + msg.senddate.split(',')[1] + '</h5>' +
            '<div style="float: right;background: #1abb9c;color: #fff" class="msgdetail">' +
            imgmsg + msg.msg +
            '</div></div><div class="media-right media-top"><a href="#">' +
            '<img class="media-object img-circle" src="' + msg.anonymityImg + '" width="40" height="40" alt="..."></a></div></div>');
    } else if(msg.userRole == 3) {
        $('[roomid=' + msg.roomID + '] .topmsg').append('<div class="media" date-show="' + msg.senddate.split(',')[0] + '"><div class="media-left media-top"><a href="#">' +
            '<img class="media-object img-circle" src="' + msg.anonymityImg + '" width="40" height="40" alt="..."></a></div>' +
            '<div class="media-body"><h5 class="media-heading">' + msg.username + '&nbsp;&nbsp;' + msg.senddate.split(',')[1] + '</h5>' +
            '<div style="float: left;background: #59bdda;color: #fff" class="msgdetail">' +
            imgmsg + msg.msg + '</div></div></div>');

    }else {
        $('[roomid=' + msg.roomID + '] .topmsg').append('<div class="media" date-show="' + msg.senddate.split(',')[0] + '"><div class="media-left media-top"><a href="#">' +
            '<img class="media-object img-circle" src="' + msg.anonymityImg + '" width="40" height="40" alt="..."></a></div>' +
            '<div class="media-body"><h5 class="media-heading">' + msg.username + '&nbsp;&nbsp;' + msg.senddate.split(',')[1] + '</h5>' +
            '<div style="float: left;background: #fff;" class="msgdetail">' +
            imgmsg + msg.msg + '</div></div></div>');

    }

	var div = document.getElementById(msg.roomID).firstElementChild.firstElementChild;
	console.log(div);
	div.scrollTop = div.scrollHeight;
	var roomlist = [];
	var phonelist=[]
	var roomindex = '';
	var activeroom = $('.socketroom.active').attr('roomid');
	var str3 = '';
	for(var k = 0; k < $('.socketroom').length; k++) {
		if($($('.socketroom')[k]).attr('roomid') == msg.roomID) {
			roomindex = k;
		}
		roomlist.push($($('.socketroom')[k]).attr('roomid'));
		phonelist.push($($('.socketroom')[k]).attr('roomname'));
	}
	console.log(roomlist);
	var roomlist2 = [];
	var phonelist2 = [];
	roomlist2 = roomlist;
	phonelist2 = phonelist;
	roomlist2.unshift(roomlist[roomindex]);
	phonelist2.unshift(phonelist[roomindex]);
	roomlist2.splice(roomindex + 1, 1);
	phonelist2.splice(roomindex + 1, 1);
	// roomlist2=roomlist2.splice(roomindex+1,1);
	for(var b = 0; b < roomlist2.length; b++) {
		if(roomlist2[b] == activeroom) {
			str3 += '<li role="presentation" class="socketroom active" roomid="' + roomlist2[b] + '" roomname="'+phonelist2[b]+'"><a  href="#' + roomlist2[b] + '" aria-controls="' + roomlist2[b] + '" role="tab" data-toggle="tab" roomname="'+phonelist2[b]+'">' +phonelist2[b]+ ' <span class="badge2"></span></a></li>';

		} else {
			str3 += '<li role="presentation" class="socketroom" roomid="' + roomlist2[b] + '" roomname="'+phonelist2[b]+'"><a  href="#' + roomlist2[b] + '" aria-controls="' + roomlist2[b] + '" role="tab" data-toggle="tab" roomname="'+phonelist2[b]+'">' +phonelist2[b]+' <span class="badge2"></span></a></li>';

		}
	}
	$('.myroom').html(str3);
	$('.socketroom a[aria-controls="' + msg.roomID + '"] span').text('.');
	$('.socketroom.active a[aria-controls="' + msg.roomID + '"] span').text('');
	$('[roomid=' + msg.roomID + '] .message').focus();
	$('.topmsg img').on('click', function(e) {
		console.log($(this).attr('src'));
		stopPropagation(e);
		$('.Reading_pictures_show').html('');
		$('.Reading_pictures_show').append('<img src="' + $(this).attr('src') + '" width="100%">');
		$('.Reading_pictures_show').css('display', 'block');
	})
});

function sendmsg(e) {
	console.log($(e.target).attr('roomid'));
	if($.trim($('[roomid=' + $(e.target).attr('roomid') + '] .message').val()) == "") {
		alert('输入不能为空,请重新输入');
	} else {
		if($('[roomid=' + $(e.target).attr('roomid') + '] .message').val().length > 500) {
			alert('发送消息内容超长，请分条发送。');
		} else {
			socket.emit('message', {
				msg: $('[roomid=' + $(e.target).attr('roomid') + '] .message').val(), //发送内容 String
				roomID: $(e.target).attr('roomid'), //房间名String
				username: msg.username, //发送者id String
				type: 'msg', //消息类型 String
				b: 1,
				anonymityImg: "https://xcximg.juseyun.com/Images/doctor.png",
				headerimg: null,
				color: false,
				to: null,
				imggs: "",
				id: 0,
				is: 2,
				token: msg.token,
                userRole:msg.userrole
			});
			$('[roomid=' + $(e.target).attr('roomid') + '] .message').val('');
			$('[roomid=' + $(e.target).attr('roomid') + '] .message').focus();

		}

	}

}

function leaveroom(e) {
	var t = confirm('是否离开房间？')
	if(t) {
		socket.emit('leave', {
			roomID: $(e.target).attr('roomid'),
			username: msg.username

		});
		$.ajax({
			url: url + '/service/lrem_Roominfo',
			type: 'post',
			data: {
				userid: msg.userid,
				roomID: $(e.target).attr('roomid'),
				roomID:JSON.stringify({
					roomID:$(e.target).attr('roomid'),
					roomname:$(e.target).attr('roomname')
				})
			},
			headers: {
				'Authorization': 'Bearer ' + msg.token
			},
			success: function(d2) {
				$("[aria-controls='" + $(e.target).attr('roomid') + "']").parent('.msgbox').next().addClass('active');
				$("[aria-controls='" + $(e.target).attr('roomid') + "']").parent('.msgbox').next().siblings('.msgbox').removeClass('active');
				$('#' + $(e.target).attr('roomid')).next().addClass('active');
				$('#' + $(e.target).attr('roomid')).next().siblings('').removeClass('active');
				$('li[roomid="' + $(e.target).attr('roomid') + '"]').remove();
				$('#' + $(e.target).attr('roomid')).remove();
			}
		})
	}
}

function joinroom(e) {
	window.top.document.getElementsByClassName('invitelist')[0].innerHTML =
		parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) - 1;
	if(parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) > 0) {
		window.top.document.getElementsByClassName('invitelist')[0].style.display = 'inline-block';
	} else {
		window.top.document.getElementsByClassName('invitelist')[0].style.display = 'none';
	}
	var _id = $(e.target).attr('_id');
	$('.topmsg').html('');
	$.ajax({
		url: url + '/service/ficationinvite',
		type: 'post',
		data: {
			id: msg.userid,
			_id: _id,
			state: 1
		},
		headers: {
			'Authorization': 'Bearer ' + msg.token
		},
		success: function(d) {
			$.ajax({
				url: url + '/service/doctor_Roominfo',
				type: 'post',
				data: {
					userid: msg.userid,
					roomID:JSON.stringify({
					  roomID:$(e.target).attr('roomid'),
					  roomname:$(e.target).attr('roomname')
					})
				},
				headers: {
					'Authorization': 'Bearer ' + msg.token
				},
				success: function(d2) {
//					socket.emit('joinroom', {
//						roomID: $(e.target).attr('roomid'),
//						username: msg.username,
//						sex: 0,
//						userrole: msg.userrole,
//						token: msg.token
//
//					});
					socket.emit('inviterooms', {
						res: true,
						socket_id: $(e.target).attr('socketid')
					});
					$('#home').removeClass('active');
					$('#profile').addClass('active');
					$('.nav.nav-tabs').children().first().removeClass('active');
					$('.nav.nav-tabs').children().first().next().addClass('active');
					$("[aria-controls='" + $(e.target).attr('roomid') + "']").parent('.msgbox').addClass('active');
					$("[aria-controls='" + $(e.target).attr('roomid') + "']").parent('.msgbox').siblings('.msgbox').removeClass('active');
					$('#' + $(e.target).attr('roomid')).addClass('active');
					$('#' + $(e.target).attr('roomid')).siblings('').removeClass('active');
					$(e.target).parent().html('<span style="color: green">已接受</span>');
					$.ajax({
						url: url + '/service/find_Roominfo',
						type: 'post',
						data: {
							userid: msg.userid
						},
						headers: {
							'Authorization': 'Bearer ' + msg.token
						},
						success: function(data) {
							console.log(data);
							var str = '';
							var str2 = '';
							var str3 = '';
                            if(data.content.length>0){
                                for(var b = 0; b < data.content.length; b++) {
                                    if(data.content[b] == mysocketid) {

                                    } else {
                                        if(data.content[b] == $(e.target).attr('roomid')) {
                                            str += '<li role="presentation" class="socketroom active" roomid="' + data.content[b].roomID + '" roomname="'+data.content[b].roomname+'"><a href="#' + data.content[b].roomID + '" aria-controls="' + data.content[b].roomID + '" role="tab" data-toggle="tab">' + data.content[b].roomname + ' <span class="badge2"></span></a></li>';
                                            str2 += '<div role="tabpanel" class="tab-pane active" id="' + data.content[b].roomID + '"><div class="col-md-5 col-sm-5 col-lg-5 msgbox" roomid="' + data.content[b] + '">' +
                                                '<div class="topmsg"></div>' +
                                                '<div class="txtmsg"><form id="' + roomid + '" enctype="multipart/form-data">' +
                                                '<a href="javascript:;" class="file"><input type="file" name="Filedata" id="" class="upload" roomid="' + data.content[b].roomID + '"></a></form><textarea class="message" placeholder="在此输入"></textarea></div>' +
                                                '<div class="text-right" style="height: 35px;background: #fff;"><button  type="button" class="over" roomid="' + data.content[b].roomID + '" roomname="'+data.content[b].roomname+'" onclick="leaveroom(event)">退出聊天</button>' +
                                                '<button  type="button" class="send" roomid="' + data.content[b].roomID + '" onclick="sendmsg(event)">发送</button> </div> </div><div class="col-md-5 col-sm-5 col-lg-5 msgbox msgboxdd">' +
                                                '' +
                                                '</div></div>';
                                        } else {
                                            str += '<li role="presentation" class="socketroom" roomid="' + data.content[b].roomID + '" roomname="'+data.content[b].roomname+'"><a href="#' + data.content[b].roomID + '" aria-controls="' + data.content[b].roomID + '" role="tab" data-toggle="tab">' + data.content[b].roomname + ' <span class="badge2"></span></a></li>';
                                            str2 += '<div role="tabpanel" class="tab-pane" id="' + data.content[b].roomID + '"><div class="col-md-5 col-sm-5 col-lg-5 msgbox" roomid="' + data.content[b].roomID+ '">' +
                                                '<div class="topmsg"></div>' +
                                                '<div class="txtmsg"><form id="' + roomid + '" enctype="multipart/form-data">' +
                                                '<a href="javascript:;" class="file"><input type="file" name="Filedata" class="upload" roomid="' + data.content[b].roomID + '"></a></form><textarea class="message" placeholder="在此输入"></textarea></div>' +
                                                '<div class="text-right" style="height: 35px;background: #fff;"><button  type="button" class="over" roomid="' + data.content[b].roomID + '" roomname="'+data.content[b].roomname+'" onclick="leaveroom(event)">退出聊天</button>' +
                                                '<button  type="button" class="send" roomid="' + data.content[b].roomID + '" onclick="sendmsg(event)">发送</button> </div> ' +
                                                '</div><div class="col-md-5 col-sm-5 col-lg-5 msgbox msgboxdd">' +
                                                '' +
                                                '</div></div>';
                                        }
                                    }
                                    $.ajax({
                                        url: url + '/service/findchat ',
                                        type: 'post',
                                        data: {
                                            roomID: $(e.target).attr('aria-controls'),
                                            username: msg.username, //用户名
                                            limit: 20,
                                            page: 1
                                        },
                                        headers: {
                                            'Authorization': 'Bearer ' + msg.token
                                        },
                                        success: function(da) {
                                            console.log(da);
                                            $('[roomid=' + $(e.target).attr('aria-controls') + '] .topmsg').html('');
                                            for(var c = 0; c < da.data.length; c++) {
                                                var imgmsg = (da.data[c].imggs == null || da.data[c].imggs == '') ? '' : '<img src="' + da.data[c].imggs + '" width="100%" alt="">';
                                                var datestr = '';
                                                if(c > 0) {
                                                    if(da.data[c].senddate.split(",")[0] != da.data[c - 1].senddate.split(",")[0]) {
                                                        datestr = da.data[c - 1].senddate.split(",")[0];
                                                        $('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="text-center">' + datestr + '</div>');
                                                    }
                                                } else {
                                                    datestr = date_cc;
                                                    $('div[date-show="' + da.data[c].senddate.split(',')[0] + '"]').length == 0 ?
                                                        $('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="text-center">' + datestr + '</div>') : '';
                                                }
                                                if(da.data[c].userRole == 1||da.data[c].userRole == 6) {
                                                    $('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="media" date-show="' + da.data[c].senddate.split(',')[0] + '">' +
                                                        '<div class="media-body"><h5 class="media-heading text-right">' + da.data[c].username + '&nbsp;&nbsp;' + da.data[c].senddate.split(',')[1] + '</h5>' +
                                                        '<div style="float: right;background: #1abb9c;color: #fff" class="msgdetail">' +
                                                        imgmsg + da.data[c].msg +
                                                        '</div></div><div class="media-right media-top"><a href="#">' +
                                                        '<img class="media-object img-circle" src="' + da.data[c].anonymityImg + '" width="40" height="40" alt="..."></a></div></div>');
                                                } else if(da.data[c].userRole == 3) {
                                                    $('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="media" date-show="' + da.data[c].senddate.split(',')[0] + '"><div class="media-left media-top"><a href="#">' +
                                                        '<img class="media-object img-circle" src="' + da.data[c].anonymityImg + '" width="40" height="40" alt="..."></a></div>' +
                                                        '<div class="media-body"><h5 class="media-heading">' + da.data[c].username + '&nbsp;&nbsp;' + da.data[c].senddate.split(',')[1] + '</h5>' +
                                                        '<div style="float: left;background: #59bdda;color: #fff" class="msgdetail">' +
                                                        imgmsg + da.data[c].msg + '</div></div></div>');

                                                }else {
                                                    $('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="media" date-show="' + da.data[c].senddate.split(',')[0] + '"><div class="media-left media-top"><a href="#">' +
                                                        '<img class="media-object img-circle" src="' + da.data[c].anonymityImg + '" width="40" height="40" alt="..."></a></div>' +
                                                        '<div class="media-body"><h5 class="media-heading">' + da.data[c].username + '&nbsp;&nbsp;' + da.data[c].senddate.split(',')[1] + '</h5>' +
                                                        '<div style="float: left;background: #fff;" class="msgdetail">' +
                                                        imgmsg + da.data[c].msg + '</div></div></div>');

                                                }

                                            }
                                            date_cc = da.data[da.data.length - 1].senddate.split(",")[0];
                                            $('[roomid=' + $(e.target).attr('aria-controls') + '] .topmsg').prepend('<div class="text-center"><a class="btn findmore" onclick="findmore(event)" data-page="1">查看更多</a></div>')
                                            var div = document.getElementById($(e.target).attr('aria-controls')).firstElementChild.firstElementChild;
                                            console.log(div);
                                            div.scrollTop = div.scrollHeight;
                                            $('.topmsg img').on('click', function(e) {
                                                console.log($(this).attr('src'));
                                                stopPropagation(e);
                                                $('.Reading_pictures_show').html('');
                                                $('.Reading_pictures_show').append('<img src="' + $(this).attr('src') + '" width="100%">');
                                                $('.Reading_pictures_show').css('display', 'block');
                                            })
                                        }
                                    });
                                }
                            }else{
                                str = '<li class="text-center"><img src="../../img/empty-s.png" alt="" style="margin-top: 200px"></li>';
                                str2 += '<div><div class="col-md-5 col-sm-5 col-lg-5 msgbox">' +
                                    '<div class="topmsg"></div>' +
                                    '<div class="txtmsg"><form enctype="multipart/form-data">' +
                                    '<a href="javascript:;" class="file"><input type="file" name="Filedata" id="" class="upload"></a></form><textarea placeholder="在此输入"></textarea></div>' +
                                    '<div class="text-right" style="height: 35px;background: #fff;"><button  type="button" class="over" >退出聊天</button>' +
                                    '<button  type="button" class="send"  >发送</button> </div> </div><div class="col-md-5 col-sm-5 col-lg-5 msgbox msgboxdd">' +
                                    '' +
                                    '</div></div>';
                            }

							$('.myroom').html(str);
							$('.roombox').html(str2);
							$('.msgbox').css('height', '600px');
							$('.socketroom a').on('click', function(e) {
								e.preventDefault();
								date_cc = '';
								var rid = $(e.target).attr('aria-controls');
								if(canemit) {
									socket.emit('joinroom', {
										roomID: $(e.target).attr('aria-controls'),
										username: msg.username,
										sex: 0,
										userrole: msg.userrole,
										token: msg.token

									});
								} else {
									canemit = false;
								}
								canemit = false;
								$.ajax({
									url: url + '/service/findchat ',
									type: 'post',
									data: {
										roomID: $(e.target).attr('aria-controls'),
										username: msg.username, //用户名
										limit: 20,
										page: 1
									},
									headers: {
										'Authorization': 'Bearer ' + msg.token
									},
									success: function(da) {
										console.log(da);
										$('[roomid=' + $(e.target).attr('aria-controls') + '] .topmsg').html('');
										for(var c = 0; c < da.data.length; c++) {
											var imgmsg = (da.data[c].imggs == null || da.data[c].imggs == '') ? '' : '<img src="' + da.data[c].imggs + '" width="100%" alt="">';
											var datestr = '';
											if(c > 0) {
												if(da.data[c].senddate.split(",")[0] != da.data[c - 1].senddate.split(",")[0]) {
													datestr = da.data[c - 1].senddate.split(",")[0];
													$('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="text-center">' + datestr + '</div>');
												}
											} else {
												datestr = date_cc;
												$('div[date-show="' + da.data[c].senddate.split(',')[0] + '"]').length == 0 ?
													$('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="text-center">' + datestr + '</div>') : '';
											}
                                            if(da.data[c].userRole == 1||da.data[c].userRole == 6) {
                                                $('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="media" date-show="' + da.data[c].senddate.split(',')[0] + '">' +
                                                    '<div class="media-body"><h5 class="media-heading text-right">' + da.data[c].username + '&nbsp;&nbsp;' + da.data[c].senddate.split(',')[1] + '</h5>' +
                                                    '<div style="float: right;background: #1abb9c;color: #fff" class="msgdetail">' +
                                                    imgmsg + da.data[c].msg +
                                                    '</div></div><div class="media-right media-top"><a href="#">' +
                                                    '<img class="media-object img-circle" src="' + da.data[c].anonymityImg + '" width="40" height="40" alt="..."></a></div></div>');
                                            } else if(da.data[c].userRole == 3) {
                                                $('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="media" date-show="' + da.data[c].senddate.split(',')[0] + '"><div class="media-left media-top"><a href="#">' +
                                                    '<img class="media-object img-circle" src="' + da.data[c].anonymityImg + '" width="40" height="40" alt="..."></a></div>' +
                                                    '<div class="media-body"><h5 class="media-heading">' + da.data[c].username + '&nbsp;&nbsp;' + da.data[c].senddate.split(',')[1] + '</h5>' +
                                                    '<div style="float: left;background: #59bdda;color: #fff" class="msgdetail">' +
                                                    imgmsg + da.data[c].msg + '</div></div></div>');

                                            }else {
                                                $('[roomid=' + da.data[c].roomID + '] .topmsg').prepend('<div class="media" date-show="' + da.data[c].senddate.split(',')[0] + '"><div class="media-left media-top"><a href="#">' +
                                                    '<img class="media-object img-circle" src="' + da.data[c].anonymityImg + '" width="40" height="40" alt="..."></a></div>' +
                                                    '<div class="media-body"><h5 class="media-heading">' + da.data[c].username + '&nbsp;&nbsp;' + da.data[c].senddate.split(',')[1] + '</h5>' +
                                                    '<div style="float: left;background: #fff;" class="msgdetail">' +
                                                    imgmsg + da.data[c].msg + '</div></div></div>');

                                            }

										}
										date_cc = da.data[da.data.length - 1].senddate.split(",")[0];
										$('[roomid=' + $(e.target).attr('aria-controls') + '] .topmsg').prepend('<div class="text-center"><a class="btn findmore" onclick="findmore(event)" data-page="1">查看更多</a></div>')
										var div = document.getElementById($(e.target).attr('aria-controls')).firstElementChild.firstElementChild;
										console.log(div);
										div.scrollTop = div.scrollHeight;
										$('.topmsg img').on('click', function(e) {
											console.log($(this).attr('src'));
											stopPropagation(e);
											$('.Reading_pictures_show').html('');
											$('.Reading_pictures_show').append('<img src="' + $(this).attr('src') + '" width="100%">');
											$('.Reading_pictures_show').css('display', 'block');
										})
									}
								});
							})
							$('.upload').on('change', function() {
								var me = this;
								console.log($(this));
								if(this.files) {
									//读取图片数据
									var f = this.files[0];
									var reader = new FileReader();
									reader.onload = function(e) {
										var data = e.target.result;
										//加载图片获取图片真实宽度和高度
										var image = new Image();
										image.onload = function() {
											var width = image.width;
											var height = image.height;
											console.log(width + '======' + height + "=====" + f.size);
											if(f.size < (5 * 1024 * 1024)) {
												var rid = $(me).attr('roomid');
												var formData = new FormData();
												formData.append('files', $(me)[0].files[0]);
												$.ajax({
													url: "https://xcx.juseyx.cn/service/uploadimg",
													type: "post",
													cache: false,
													data: formData,
													processData: false,
													contentType: false,
													success: function(res) {
														console.log(res);
														if(res.code == 200) {
															socket.emit('message', {
																msg: '', //发送内容 String
																roomID: rid, //房间名String
																username: msg.username, //发送者id String
																type: 'msg', //消息类型 String
																b: 1,
																anonymityImg: "https://xcximg.juseyun.com/Images/doctor.png",
																headerimg: null,
																color: false,
																to: null,
																imggs: res.data,
																id: 0,
																is: 2,
																token: msg.token,
                                                                userRole:msg.userrole
															});

														}
													}
												})
											} else {
												alert('图片过大请重新选择')
											}
										};
										image.src = data;
									};
									reader.readAsDataURL(f);
								} else {
									var image = new Image();
									image.onload = function() {
										var width = image.width;
										var height = image.height;
										var fileSize = image.fileSize;
										alert(width + '======' + height + "=====" + fileSize);
									}
									image.src = this.value;
								}

							});
							$("textarea").keydown(function(e) {
								var roomid = $(e.target).parent().next().children().attr('roomid');
								if(e.keyCode == 13) {
									e.returnValue = $.trim($('[roomid=' + roomid + '] .message').val());
									e.preventDefault();
									if(e.returnValue == "") {
										alert('输入不能为空,请重新输入');
									} else {
										if($('[roomid=' + roomid + '] .message').val().length > 500) {
											alert('发送消息内容超长，请分条发送。');
										} else {
											socket.emit('message', {
												msg: $('[roomid=' + roomid + '] .message').val(), //发送内容 String
												roomID: roomid, //房间名String
												username: msg.username, //发送者id String
												type: 'msg', //消息类型 String
												b: 1,
												anonymityImg: "https://xcximg.juseyun.com/Images/doctor.png",
												headerimg: null,
												color: false,
												to: null,
												imggs: "",
												id: 0,
												is: 2,
												token: msg.token,
                                                userRole:msg.userrole
											});
											$('[roomid=' + roomid + '] .message').val('');
											$('[roomid=' + roomid + '] .message').focus();

										}

									}
								}
							});
						}
					})
				}
			});
		}
	})
}

function notjoin(e) {
	socket.emit('inviterooms', {
		res: false,
		socket_id: $(e.target).attr('socketid')
	});
	$.ajax({
		url: url + '/service/ficationinvite ',
		type: 'post',
		data: {
			id: msg.userid,
			_id: $(e.target).attr('_id'),
			state: 2
		},
		headers: {
			'Authorization': 'Bearer ' + msg.token
		},
		success: function(d) {
			$(e.target).parent().html('<span style="color: red">已拒绝</span>')
			window.top.document.getElementsByClassName('invitelist')[0].innerHTML =
				parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) - 1;
			if(parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) > 0) {
				window.top.document.getElementsByClassName('invitelist')[0].style.display = 'inline-block';
			} else {
				window.top.document.getElementsByClassName('invitelist')[0].style.display = 'none';
			}
		}
	})
}

//撤回邀请
socket.on("UndoInvite", function(data) {
	console.log(data, 1111)
    $('#home button[roomid="' + data.data.roomID + '"]').parent().prev().children('.rightmsg').text()
        $.ajax({
            url: url + '/service/ficationinvite ',
            type: 'post',
            data: {
                id: msg.userid,
                _id: $('.bg-color button[roomid="' + data.data.roomID + '"]').attr('_id'),
                state: 3
            },
            headers: {
                'Authorization': 'Bearer ' + msg.token
            },
            success: function(d) {
                $('.bg-color button[roomid="' + data.data.roomID + '"]').parent().html('<span style="color: red">已关闭</span>');
                window.top.document.getElementsByClassName('invitelist')[0].innerHTML =
                    parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) - 1;
                if(parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) > 0) {
                    window.top.document.getElementsByClassName('invitelist')[0].style.display = 'inline-block';
                } else {
                    window.top.document.getElementsByClassName('invitelist')[0].style.display = 'none';
                }
            }
        })
});
//审核不通过通知消息
socket.on("notthrough_msg",function(data){
    console.log(data.msg+data._id[0]);
    var bottom=-200;
    var timer=setInterval(function () {
        bottom+=4;
        window.top.document.getElementById('msgnew').style.bottom=bottom+'px';
        window.top.document.getElementsByClassName('newmsg')[0].innerHTML='审核不通过';
        if(bottom>0){
            clearInterval(timer);
        }
    },47)
    window.top.document.getElementsByClassName('unfinish')[0].innerHTML=
        parseFloat(window.top.document.getElementsByClassName('unfinish')[0].innerHTML)+1;
    window.top.document.getElementsByClassName('unfinish')[0].style.display='inline-block';
})
//撤回指派
socket.on("Undo", function(data2) {
    console.log(data2, 222)
    window.top.document.getElementsByClassName('unfinish')[0].innerHTML =
        parseFloat(window.top.document.getElementsByClassName('unfinish')[0].innerHTML) - 1;
    if(parseFloat(window.top.document.getElementsByClassName('unfinish')[0].innerHTML) > 0) {
        window.top.document.getElementsByClassName('unfinish')[0].style.display = 'inline-block';
    } else {
        window.top.document.getElementsByClassName('unfinish')[0].style.display = 'none';
    }
    alert(data2.name+"撤回指派")
})

function stopPropagation(e) {
	if(e.stopPropagation)
		e.stopPropagation();
	else
		e.cancelBubble = true;
};
$(document).bind('click', function() {
	$('.Reading_pictures_show').css('display', 'none');
});
function findmore(e) {
    var p = parseInt($(e.target).attr('data-page'));
    p++;
    var rid=$('li.socketroom.active').attr('roomid');
    $(e.target).parent().remove();
    $.ajax({
        url: url + '/service/findchat ',
        type: 'post',
        data: {
            roomID: rid,
            username: msg.username, //用户名
            limit: 20,
            page: p
        },
        headers: {
            'Authorization': 'Bearer ' + msg.token
        },
        success: function(da2) {
            console.log(da2);
            if(da2.data.length == 0) {

            } else {
                date_cc = da2.data[da2.data.length - 1].senddate.split(",")[0];
                for(var c = 0; c < da2.data.length; c++) {
                    var imgmsg = (da2.data[c].imggs == null || da2.data[c].imggs == '') ? '' : '<img src="' + da2.data[c].imggs + '" width="100%" alt="">'
                    var datestr = '';
                    if(c > 0) {
                        if(da2.data[c].senddate.split(",")[0] != da2.data[c - 1].senddate.split(",")[0]) {
                            datestr = da2.data[c - 1].senddate.split(",")[0];
                            $('[roomid=' + da2.data[c].roomID + '] .topmsg').prepend('<div class="text-center">' + datestr + '</div>');
                        }
                    } else {
                        datestr = date_cc;
                        $('div[date-show="' + da2.data[c].senddate.split(',')[0] + '"]').length == 0 ?
                            $('[roomid=' + da2.data[c].roomID + '] .topmsg').prepend('<div class="text-center">' + datestr + '</div>') : '';
                    }
                    if(da2.data[c].userRole == 1||da2.data[c].userRole == 6) {
                        $('[roomid=' + da2.data[c].roomID + '] .topmsg').prepend('<div class="media" date-show="' + da2.data[c].senddate.split(',')[0] + '">' +
                            '<div class="media-body"><h5 class="media-heading text-right">' + da2.data[c].username + '&nbsp;&nbsp;' + da2.data[c].senddate.split(',')[1] + '</h5>' +
                            '<div style="float: right;background: #1abb9c;color: #fff" class="msgdetail">' +
                            imgmsg + da2.data[c].msg +
                            '</div></div><div class="media-right media-top"><a href="#">' +
                            '<img class="media-object img-circle" src="' + da2.data[c].anonymityImg + '" width="40" height="40" alt="..."></a></div></div>');
                    } else if(da2.data[c].userRole == 3) {
                        $('[roomid=' + da2.data[c].roomID + '] .topmsg').prepend('<div class="media" date-show="' + da2.data[c].senddate.split(',')[0] + '"><div class="media-left media-top"><a href="#">' +
                            '<img class="media-object img-circle" src="' + da2.data[c].anonymityImg + '" width="40" height="40" alt="..."></a></div>' +
                            '<div class="media-body"><h5 class="media-heading">' + da2.data[c].username + '&nbsp;&nbsp;' + da2.data[c].senddate.split(',')[1] + '</h5>' +
                            '<div style="float: left;background: #59bdda;color: #fff" class="msgdetail">' +
                            imgmsg + da2.data[c].msg + '</div></div></div>');

                    }else {
                        $('[roomid=' + da2.data[c].roomID + '] .topmsg').prepend('<div class="media" date-show="' + da2.data[c].senddate.split(',')[0] + '"><div class="media-left media-top"><a href="#">' +
                            '<img class="media-object img-circle" src="' + da2.data[c].anonymityImg + '" width="40" height="40" alt="..."></a></div>' +
                            '<div class="media-body"><h5 class="media-heading">' + da2.data[c].username + '&nbsp;&nbsp;' + da2.data[c].senddate.split(',')[1] + '</h5>' +
                            '<div style="float: left;background: #fff;" class="msgdetail">' +
                            imgmsg + da2.data[c].msg + '</div></div></div>');

                    }

                }
                $('[roomid=' + rid + '] .topmsg').prepend('<div class="text-center"><a class="btn findmore" onclick="findmore(event)" data-page="' + p + '">查看更多</a></div>')
            }
        }
    })
}